<?php
class cJSValidation
	{
	public $validation_array;
	public $db;

	function generateJSValidation($master_code="")
		{
		$this->generateValidationArray($master_code);
		$script=$this->createScript();
		return $script;
		}
	function generateValidationArray($master_code="")
		{
		$this->db->query="SELECT master_column_display,master_column_name,validation_pattern FROM master_columns WHERE validation_pattern<>'' AND  master_code='$master_code'";
		$this->db->runQuery();
		$validation_pattern_array=$this->db->returnArrays();
		$this->validation_array ="validation_element = new Array(); ";
		$this->validation_array.="validation_pattern = new Array(); ";
		$this->validation_array.="validation_element_name= new Array(); ";
		for($i=0,$cnt=count($validation_pattern_array);$i<$cnt;$i++)
			{
			$this->validation_array.="validation_element_name[$i]='".$validation_pattern_array[$i]["master_column_display"]."'; ";
			$this->validation_array.="validation_element[$i]='".$validation_pattern_array[$i]["master_column_name"]."'; ";
			$this->validation_array.="validation_pattern[$i]='".$validation_pattern_array[$i]["validation_pattern"]."'; ";
			}
		}

	function createScript()
		{
		$JS_script ="<script language=\"javascript\"> ";
		$JS_script.="function validateEntry() ";
		$JS_script.="{ ";
		$JS_script.=$this->validation_array;
		$JS_script.="for(i=0;i<validation_element.length;i++)";
		$JS_script.="{";
		$JS_script.="var reg=new RegExp(validation_pattern[i]); ";
		$JS_script.="var e=eval(document.getElementById(validation_element[i]));";
		$JS_script.="entered_value=e.value;";
		$JS_script.="if(!reg.test(entered_value))";
		$JS_script.="{";
		$JS_script.="    alert(validation_element_name[i]+': Enter Correct Value'); ";
		$JS_script.="    e.focus();";
		$JS_script.="    return false;";
		$JS_script.="}";
		$JS_script.="}";
		$JS_script.="}";
		$JS_script.="</script>";
		return $JS_script;
		}
	}

?>
